﻿using System;
using System.Collections;
using System.Windows;
using System.Windows.Data;
using System.Windows.Navigation;
using Project5188.BUS;
using Project5188.Client.ClientCodes;

namespace Project5188.Client
{
    /// <summary>
    /// Interaction logic for ParameterPage.xaml
    /// </summary>
    public partial class ParameterPage
    {
        private CollectionViewSource _resources;

        public ParameterPage() : base(Extensions.PageNames.ParameterPage)
        {
            InitializeComponent();

            pagingControl.DataChanged += PagingControlDataChanged;

            DataBind();
        }

        void PagingControlDataChanged(object sender, EventArgs e)
        {
            if (_resources != null) _resources.Source = pagingControl.Paging.GetDataList();
            if (_resources != null) dgParameters.ItemsSource = _resources.Source as IEnumerable;
        }

        private void DataBind()
        {
            var parameters = new Parameters();
            _resources = Resources["parametersViewSource"] as CollectionViewSource;
            pagingControl.SetDataSource(parameters.GetEntityList(), 1, 10);
            if (_resources != null) _resources.Source = pagingControl.Paging.GetDataList();
            if (_resources != null) dgParameters.ItemsSource = _resources.Source as IEnumerable;
        }

        private void BtnDetailClick(object sender, RoutedEventArgs e)
        {
            var param = (parameter)dgParameters.SelectedItem;
            var detail = new DetailWindow();
            DetailView<parameter> parameterDetail = new Parameters {Entity = param};
            detail.SetEntity(parameterDetail, Extensions.PageNames.ParameterPage.ToString());
            detail.Show();
        }

        private void BtnUpdateClick(object sender, RoutedEventArgs e)
        {
            var param = (parameter)dgParameters.SelectedItem;
            var update = new UpdateWindow();
            update.Closed += UpdateClosed;
            DetailView<parameter> parameterDetail = new Parameters();
            update.SetEntity(parameterDetail, Extensions.PageNames.ParameterPage.ToString(), param.ID);
            update.Show();
        }

        private void BtnDeleteClick(object sender, RoutedEventArgs e)
        {
            var param = (parameter)dgParameters.SelectedItem;
            DetailView<parameter> parameterDetail = new Parameters();
            GenerateDeleteDialog(parameterDetail, param.ID);
        }

        private void BtnAddClick(object sender, RoutedEventArgs e)
        {
            var insert = new InsertWindow();
            insert.Closed += InsertClosed;
            DetailView<parameter> parameterDetail = new Parameters();
            insert.SetEntity(parameterDetail, Extensions.PageNames.ParameterPage.ToString());
            insert.Show();
        }

        private void BtnValueClick(object sender, RoutedEventArgs e)
        {
            var param = (parameter)dgParameters.SelectedItem;
            var paramValue = new ParameterValuePage(param.ID);
            var nav = NavigationService.GetNavigationService(this);
            if (nav != null) nav.Navigate(paramValue);
        }

        public override bool Search()
        {
            if (base.Search())
            {
                var parameters = new Parameters();
                _resources = Resources["parametersViewSource"] as CollectionViewSource;
                pagingControl.SetDataSource(parameters.GetEntityListByParameters(txtParameterName.Text), 1, 10);
                if (_resources != null) _resources.Source = pagingControl.Paging.GetDataList();
                if (_resources != null) dgParameters.ItemsSource = _resources.Source as IEnumerable;
            }
            else if (TransactionInfo.TransactionId.HasValue)
            {
                var parameters = new Parameters();
                _resources = Resources["parametersViewSource"] as CollectionViewSource;
                pagingControl.SetDataSource(parameters.GetEntityById(TransactionInfo.TransactionId.Value), 1, 10);
                if (_resources != null) _resources.Source = pagingControl.Paging.GetDataList();
                if (_resources != null) dgParameters.ItemsSource = _resources.Source as IEnumerable;
            }

            return true;
        }
    }
}
